﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using DevExpress.XtraGrid.Views.Grid;
using WmsApp.Security;
using WmsApp.WmsService;

namespace WmsApp.Export
{
    public class ExcelImportor
    {
        public const decimal RateTax =(Decimal) 0.17;
        public static List<InStoreBillDtl> ReadGoodsCodeTypeFromExcel(string excelFile,string billID)
        {
            IExcelImport excelImport = new ExcelImport();
            return excelImport.ReadGoodsCodeTypeFromExcel(excelFile, billID);
            #region 作废
            //var inStoreBillDtls = new List<InStoreBillDtl>();
            //DataTable excelTable = null;
            //var ds = new DataSet();

            //var conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFile + ";" + "Extended Properties=Excel 8.0;");
            //conn.Open();
            //var schemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
            //string tableName = schemaTable.Rows[0][2].ToString().Trim();//获取 Excel 的表名，默认值是sheet1
            //string strSql = "select * from [" + tableName + "]";
            //var myData = new OleDbDataAdapter(strSql, conn);
            //myData.Fill(ds, tableName);

            //conn.Close();

            //excelTable = ds.Tables[tableName];

            //var client = Factory.WmsServiceFactory.GetServiceClient();

            //int idx = 0;
            //foreach (DataRow row in excelTable.Rows)
            //{
            //    if( String.IsNullOrEmpty(row["物料长代码"].ToString().Trim()))
            //    {
            //        continue;
            //    }
            //    var dtl = InStoreBillDtl.CreateInStoreBillDtl(billID);

            //    dtl.NewGoodsCode = row["物料长代码"].ToString();
            //    dtl.NewGoodsName =  row["物料名称"].ToString();
            //    //修正为不含税
            //    dtl.PriceNotTax = Math.Round(Decimal.Parse( row["物料名称单价(常用)"].ToString()),4);
            //    dtl.Quantity = Decimal.Parse( row["物料名称数量(常用)"].ToString());
            //    //修正为不含税
            //    dtl.AmountNotTax = Math.Round(Decimal.Parse(row["物料名称金额"].ToString()), 2);
            //    dtl.TaxRate = RateTax;
                             
            //    dtl.PriceTax = Math.Round( dtl.PriceNotTax*(1+RateTax),2);
            //    dtl.AmountTax = Math.Round(dtl.PriceNotTax * dtl.Quantity * (1 + RateTax), 2);
            //    dtl.Idx = ++idx;
            //    dtl.BillDtlID = Guid.NewGuid().ToString();
            //    dtl.OldGoodsCode = dtl.NewGoodsCode;
            //    dtl.SpecificationType = "";
            //    dtl.StoreTypeCode = "";
            //    dtl.StoreTypeName = "";
            //    dtl.UnitCode = "";
            //    dtl.UnitName = "";
            //    dtl.CREATETIME = DateTime.Now.Date;
            //    dtl.CREATERUSERID = UserSession.CurrentUser.USERID;
            //    dtl.CREATERNAME = UserSession.CurrentUser.USERNAME;
            //    dtl.WcfEntityState = EntityState.Added;

            //    inStoreBillDtls.Add(dtl);
  
            //    var goodsCodeType = new GoodsCodeType
            //                            {
            //                                NewGoodsCode = dtl.NewGoodsCode,
            //                                NewGoodsName = dtl.NewGoodsName,
            //                                OldGoodsCode = dtl.OldGoodsCode,
            //                                SafeStoreQty = 0,
            //                                StoreLocation = "",
            //                                StoreTypeCode = dtl.StoreTypeCode,
            //                                StoreTypeName = dtl.StoreTypeName,
            //                                UnitCode = dtl.UnitCode,
            //                                UnitName = dtl.UnitName,
            //                                ChargeID = UserSession.CurrentUser.USERID,
            //                                ChargeName = UserSession.CurrentUser.USERNAME,
            //                                GoodsClass = "",
            //                                GoodsDesc = ""
            //                            };
            //    client.AddOrValidateGoodsCodeType(goodsCodeType);
            //}

            //return inStoreBillDtls;
            #endregion
        }

        public static List<GoodsCodeType> ImportGoodsCodeTypeFromExcel(string excelFile)
        {

            IExcelImport excelImport = new ExcelImport();
            return excelImport.ImportGoodsCodeTypeFromExcel(excelFile);

            #region 作废
            //var list = new List<GoodsCodeType>();
            //DataTable excelTable = null;
            //var ds = new DataSet();

            //var conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFile + ";" + "Extended Properties=Excel 8.0;");
            //conn.Open();
            //var schemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
            //string tableName = schemaTable.Rows[0][2].ToString().Trim();//获取 Excel 的表名，默认值是sheet1
            //string strSql = "select * from [" + tableName + "]";
            //var myData = new OleDbDataAdapter(strSql, conn);
            //myData.Fill(ds, tableName);

            //conn.Close();
            //var client = Factory.WmsServiceFactory.GetServiceClient();
            //excelTable = ds.Tables[tableName];
            //int idx = 0;
            //foreach (DataRow row in excelTable.Rows)
            //{
            //    if (String.IsNullOrEmpty(row["代码"].ToString().Trim()))
            //    {
            //        continue;
            //    }
            //    var tempList =  client.GetGoodsCodeTypesByCondition(String.Format(" and NewGoodsCode = '{0}'",
            //                                                      row["代码"].ToString().Trim()));
            //    if(tempList.Count > 0)
            //    {
            //        continue;
            //    }
            //    var goodsCodeType = new GoodsCodeType();
            //    goodsCodeType.NewGoodsCode = row["代码"].ToString().Trim();
            //    goodsCodeType.NewGoodsName = row["名称"].ToString().Trim();
            //    goodsCodeType.OldGoodsCode = row["代码"].ToString().Trim();
            //    goodsCodeType.GoodsDesc = row["全名"].ToString().Trim();
            //    goodsCodeType.ChargeID = UserSession.CurrentUser.USERID;
            //    goodsCodeType.ChargeName = UserSession.CurrentUser.USERNAME;
            //    goodsCodeType.SafeStoreQty = 0;
            //    goodsCodeType.StoreLocation = "";
            //    goodsCodeType.WcfEntityState = EntityState.Added;
            //    client.SaveGoodsCodeTypes(new List<GoodsCodeType>() { goodsCodeType });
            //    list.Add(goodsCodeType);
            //}
            //return list;
            #endregion
        }

        public static List<GoodsCodeType> ImportGoodsCodeTypeFromExcel2(string excelFile)
        {
            IExcelImport excelImport = new ExcelImport();
            return excelImport.ImportGoodsCodeTypeFromExcel2(excelFile);
        }
    }
}
